import IntegrationTable, {
	IntegrationTableProps,
} from "@/components/IntegrationTable";
import { ProColumns } from "@ant-design/pro-table";
import React, { useEffect, useState } from "react";
import { Image, Radio, Cascader } from "antd";
import { classify, getClassifyListCode } from "./service";
import { DynamicFormItem } from "@/components/DynamicForm";
import { getArray, arrayToObject } from "phinney-toolkit";

// @page 商家
const Business: React.FC<any> = () => {
	// 组件是否已经卸载
	let isUnMounted = false;
	const goodsTypeList = [
		{ label: "热门兑换", value: 1 },
		{ label: "福利社", value: 2 },
		{ label: "新人商品", value: 3 },
		{ label: "游戏兑换", value: 5 },
	];

	const statusList = [
		{ label: "启用", value: 1 },
		{ label: "禁用", value: 2 },
		{
			label: "删除",
			value: -1,
		},
	];
	let [classifyList, setClassifyList] = useState<any>([]);
	let [classifyCode, setClassifyCode] = useState<any>([]);

	const getClassifyList = async () => {
		const res = await classify({
			classifyType: 0,
		});
		setClassifyList(res.data);
	};

	const getClassifyCode = async () => {
		const res = await getClassifyListCode({
			classifyId: 156,
		});

		!isUnMounted &&
			setClassifyCode(
				getArray(res?.data.records).map((item) => ({
					label: item?.propsName,
					value: item?.propsCode,
				}))
			);
	};

	// 表格项
	const columns: ProColumns[] = [
		{
			title: "bannerId",
			dataIndex: "bannerId",
			search: false,
		},
		{
			title: "标题",
			dataIndex: "title",
			hideInTable: true,
			search: false,
		},
		{
			title: "描述",
			dataIndex: "bannerDesc",
			search: false,
		},
		{
			title: "图片地址",
			dataIndex: "imgUrl",
			search: false,
			render: (_, record: any) => {
				if (record.imgUrl) {
					return (
						<Image.PreviewGroup>
							<Image
								src={record.imgUrl}
								style={{ padding: 8 }}
								width={50}
								alt=""
							/>
						</Image.PreviewGroup>
					);
				} else {
					return <div>-</div>;
				}
			},
		},
		{
			title: "跳转类型",
			dataIndex: "action",
			search: false,
			valueEnum: {
				1: "url",
				2: "路由",
				3: "图片",
			},
		},
		{
			title: "携带参数",
			dataIndex: "actionParams",
			search: false,
		},
		{
			title: "页面标识",
			dataIndex: "sysType",
		},
		{
			title: "跳转路径",
			dataIndex: "actionPage",
			search: false,
		},
		{
			title: "跳转子路由",
			dataIndex: "routerDetail",
			search: false,
		},
	];

	// 表单项
	const getFormItems = (vm: any, isEdit?: boolean): DynamicFormItem[] => {
		const { coupon } = vm.state;
		return [
			{
				type: "text",
				label: "标题",
				name: "title",
				required: true,
			},
			{
				type: "text",
				label: "描述",
				name: "bannerDesc",
				required: true,
			},
			{
				type: "upload",
				label: "图片地址",
				name: "imgUrl",
				fieldProps: {
					limit: 1,
				},
				required: true,
			},
			{
				type: "select",
				label: "跳转类型",
				name: "action",
				required: true,
				options: [
					{
						label: "url",
						value: "1",
					},
					{
						label: "路由",
						value: "2",
					},
					{
						label: "图片",
						value: "3",
					},
				],
			},
			{
				type: "text",
				label: "携带参数",
				name: "actionParams",
				required: false,
			},
			{
				type: "select",
				label: "页面标识",
				name: "sysType",
				required: true,
				options: [
					{
						label: "好物",
						value: "goods",
					},
					{
						label: "商城",
						value: "mall",
					},
					{
						label: "首页",
						value: "index",
					},
					{
						label: "活动",
						value: "activity",
					},
				],
			},
			{
				type: "text",
				label: "跳转路径",
				name: "actionPage",
				required: true,
			},
			{
				type: "text",
				label: "跳转子路由",
				name: "routerDetail",
				required: false,
			},
		];
	};

	// 一体化表格props
	const tablePorps: IntegrationTableProps = {
		rowKey: "id",
		scroll: { x: 1000 },
		columns,
		listProps: {
			url: "/banner/list",
			method: "GET",
			requestType: "params",
			paramsHandle: (params: any) => {
				return {
					...params,
				};
			},
		},
		addProps: {
			url: "/banner/add",
			modalProps: {
				formProps: {
					labelCol: { span: 7 },
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm);
				},
			},
			paramsHandle: ({ ...params }): any => {
				params.imgUrl = (
					params?.imgUrl?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
					goodsPrice: (Number(params.goodsPrice) * 100).toFixed(0),
					goodsOriginPrice: (Number(params.goodsOriginPrice) * 100).toFixed(0),
					goodsType: params.goodsType,
				};
			},
		},
		updateProps: {
			url: "/banner/edit",
			method: "POST",
			modalProps: {
				formProps: {
					labelCol: { span: 7 },
				},
				formItemsHandle: (_: any, vm: any) => {
					return getFormItems(vm, true);
				},
				formValuesHandle: async (record: any) => {
					return {
						...record,
						imgUrl: record.imgUrl
							? [
									{
										uid: "-1",
										status: "done",
										name: record.imgUrl,
										url: record.imgUrl,
									},
							  ]
							: [],
						goodsPrice: record.goodsPrice / 100,
						goodsOriginPrice: record.goodsOriginPrice / 100,
					};
				},
			},
			paramsHandle: ({ ...params }): any => {
				params.imgUrl = (
					params?.imgUrl?.map((item: any) => item.url) || []
				).join(",");
				return {
					...params,
					goodsPrice: (Number(params.goodsPrice) * 100).toFixed(0),
					goodsOriginPrice: (Number(params.goodsOriginPrice) * 100).toFixed(0),
					goodsType: params.goodsType,
				};
			},
		},
		deleteProps: {
			url: "/banner/delete",
			method: "POST",
		},
	};

	// 初始化加载数据
	useEffect(() => {
		getClassifyList();
		getClassifyCode();
		return () => {
			isUnMounted = true;
		};
	}, []);

	return <IntegrationTable {...tablePorps} />;
};

export default Business;
